﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint.Administration.Claims;

namespace Mystery.SharePoint
{
	public static class UserUtilites
	{
		/// <summary>
		/// Exteracts the traditional domain\user from a string that may include claims information sucn as i:0#.w|domain\user 
		/// </summary>
		/// <param name="userName"></param>
		/// <returns></returns>
		public static string ExtractDomainUser(string userName)
		{
            int i = userName.IndexOf('|');
            if (i > 0)
            {
                return userName.Substring(i + 1);
            }
            else
            {
                return userName;
            }
		}
        /// <summary>
        /// Makes sure that the user name contains a claim prefix
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public static String AddClaimsPrefix(string userName)
        {
            if (userName.IndexOf('|') > 0)
            {
                return userName;
            }
            SPClaimProviderManager claimManager = new SPClaimProviderManager();
            SPClaim claim = claimManager.ConvertIdentifierToClaim(userName, SPIdentifierTypes.WindowsSamAccountName);
                        
            return claimManager.EncodeClaim(claim);
        }
	}
}
